package com.example.dorm1_demo.controller;

import com.example.dorm1_demo.entity.Dormitory;
import com.example.dorm1_demo.entity.Student;
import com.example.dorm1_demo.service.DormitoryService;
import com.example.dorm1_demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//宿舍控制器类，处理与宿舍相关的HTTP请求

@Controller
@RequestMapping("/dormitory")
public class DormitoryController {
    // 注入宿舍服务类
    @Autowired
    private DormitoryService dormitoryService;
    // 注入学生服务类
    @Autowired
    private StudentService studentService;

    // 获取所有宿舍的列表
    @GetMapping("/list")
    public String listDormitories(Model model) {
        model.addAttribute("dormitories", dormitoryService.getAllDormitories());
        return "dormitory-list";
    }
    //根据宿舍号搜索宿舍
    @GetMapping("/search/number")
    public String searchByNumber(@RequestParam String number, Model model) {
        model.addAttribute("dormitories", dormitoryService.searchDormitoriesByNumber(number));
        model.addAttribute("searchNumber", number);
        return "dormitory-list";
    }

    //根据楼号搜索宿舍
    @GetMapping("/search/building")
    public String searchByBuilding(@RequestParam String building, Model model) {
        model.addAttribute("dormitories", dormitoryService.searchDormitoriesByBuilding(building));
        model.addAttribute("searchBuilding", building);
        return "dormitory-list";
    }

    //显示添加宿舍的页面
    @GetMapping("/add")
    public String addDormitoryPage() {
        return "dormitory-add";
    }

    //添加新的宿舍
    @PostMapping("/add")
    public String addDormitory(Dormitory dormitory) {
        // 初始化宿舍状态
        dormitory.setOccupied(0);
        dormitory.setStatus("空宿舍");
        dormitoryService.addDormitory(dormitory);
        return "redirect:/dormitory/list";
    }

    // 显示编辑宿舍的页面
    @GetMapping("/edit/{id}")
    public String editDormitoryPage(@PathVariable Integer id, Model model) {
        model.addAttribute("dormitory", dormitoryService.getDormitoryById(id));
        return "dormitory-edit";
    }

    // 编辑宿舍信息
    @PostMapping("/edit")
    public String editDormitory(Dormitory dormitory) {
        dormitoryService.updateDormitory(dormitory);
        return "redirect:/dormitory/list";
    }

    //  删除宿舍
    @GetMapping("/delete/{id}")
    public String deleteDormitory(@PathVariable Integer id) {
        dormitoryService.deleteDormitory(id);
        return "redirect:/dormitory/list";
    }

    //获取宿舍详情
    @GetMapping("/{id}")
    public String getDormitoryDetails(@PathVariable Integer id, Model model) {
        Dormitory dormitory = dormitoryService.getDormitoryById(id);
        List<Student> students = studentService.getStudentsByDormitoryId(id);

        model.addAttribute("dormitory", dormitory);
        model.addAttribute("students", students);
        return "dormitory-detail";
    }
}
